//
//  YourTestChatViewController.m
//  ECO
//
//  Created by zl on 2019/9/26.
//  Copyright © 2019 谷银行. All rights reserved.
//

#import "YourTestChatViewController.h"
#import "RCConVerViewController.h"
#import "DetailModel.h"
@interface YourTestChatViewController ()<RCIMUserInfoDataSource,RCIMReceiveMessageDelegate>
@property(nonatomic , strong)NSMutableArray *Array;

@end

@implementation YourTestChatViewController
- (NSMutableArray *)Array{
    if (!_Array) {
        _Array = [[NSMutableArray alloc]init];
    }
    return _Array;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //   您在点击会话列表中的聚合Cell跳转到到子会话列表时，需要将此属性设置为YES。
    self.isEnteredToCollectionViewController = YES;
    self.view.backgroundColor = [UIColor colorWithHexString:@"#FFFFFF"];
    //   数据代理
    [[RCIM sharedRCIM] setUserInfoDataSource:self];
    [self Nav];
    [self setConversationAvatarStyle:RC_USER_AVATAR_CYCLE];
    //   设置需要显示哪些类型的会话
    [self setDisplayConversationTypes:@[@(ConversationType_PRIVATE),
                                        @(ConversationType_DISCUSSION),
                                        @(ConversationType_CHATROOM),
                                        @(ConversationType_GROUP),
                                        @(ConversationType_APPSERVICE),
                                        @(ConversationType_SYSTEM)]];
    //   设置需要将哪些类型的会话在会话列表中聚合显示
    [self setCollectionConversationType:@[@(ConversationType_DISCUSSION),
                                          @(ConversationType_SYSTEM)]];
    self.conversationListTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.conversationListTableView.backgroundColor = [UIColor colorWithHexString:@"#FFFFFF"];
    NSArray *conversationList = [[RCIMClient sharedRCIMClient]
                                 getConversationList:@[@(ConversationType_PRIVATE)]];
    for (RCConversation *conversation in conversationList) {
        NSLog(@"会话类型：%lu，目标会话ID：%@ , %@", (unsigned long)conversation.conversationType, conversation.targetId,conversation.lastestMessage.senderUserInfo.name);
           //   循环添加数组
        [self.Array addObject:conversation];
    }
}
// 导航条
- (void)Nav{
    UILabel *label = [[UILabel alloc]init];
    label.text = @"消息";
    label.font = [UIFont systemFontOfSize:18 * autoSizeScaleX weight:1.5 * autoSizeScaleX];
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:label];
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
}
-(void)getUserInfoWithUserId:(NSString *)userId completion:(void (^)(RCUserInfo *))completion{
   RCUserInfo *user = [[RCUserInfo alloc]init];
    for (int i = 0; i<self.Array.count; i++) {
        RCConversation *conversation = self.Array[i];
        if ([userId isEqualToString:conversation.targetId]) {
            user.userId = conversation.targetId;// id
            NSDictionary *dic = @{
                                @"userId":conversation.targetId
                                };
            [HttpsTool get:@"/kf/eco/user/findUser" params:dic success:^(id response) {
                NSLog(@"获取用户信息及粉丝数 : %@",response);
                NSDictionary *dict = [[NSDictionary alloc]init];
                DetailModel *model = [DetailModel yy_modelWithJSON:response];
                dict = [model.data.user yy_modelToJSONObject];
                user.name = dict[@"userName"];
                user.portraitUri = dict[@"userImgUrl"];//   头像的url
                [[RCIM sharedRCIM]refreshUserInfoCache:user withUserId:userId];
                // 携带当前对话用户的信息
                [RCIM sharedRCIM].currentUserInfo = [[RCUserInfo alloc]initWithUserId:conversation.targetId name:dict[@"userName"] portrait:dict[@"userImgUrl"]];
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.conversationListTableView reloadData];
                });
            } failure:^(NSError *error) {
                NSLog(@"%@",error);
            }];
            
        }else{
        }
    }
    return completion(user);
}
//重写RCConversationListViewController的onSelectedTableRow事件
- (void)onSelectedTableRow:(RCConversationModelType)conversationModelType
         conversationModel:(RCConversationModel *)model
               atIndexPath:(NSIndexPath *)indexPath {
    RCConVerViewController *conversationVC = [[RCConVerViewController alloc]init];
    conversationVC.conversationType = model.conversationType;
    conversationVC.targetId = model.targetId;
    conversationVC.title = model.conversationTitle;
    [self.navigationController pushViewController:conversationVC animated:YES];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    /*!
        从数据库中重新读取会话列表数据，并刷新会话列表
        @warning 从数据库中重新读取并刷新，会比较耗时，请谨慎使用。
    */
    [self refreshConversationTableViewIfNeeded];
    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden = NO;
    [self.conversationListTableView reloadData];
}
@end
